package domain;

import domain.shop.Product;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlTransient;
import java.util.Calendar;
import java.util.Date;

/**
 * Comment under products.
 */

@Entity
@Table(name = "COMMENTS")
@NamedQueries({
        @NamedQuery(name = "Comment.getById",
                query = " from Comment where id = :id"),
        @NamedQuery(name = "Comment.getByProductId",
                query = " from Comment where product.id = :productId"),
        @NamedQuery(name = "Comment.countByProductId",
                query = "select count(comment) from Comment comment where comment.product.id = :id")
})
public class Comment extends AbstractEntity {

    @Basic
    @Column(name = "comment")
    protected String comment = "";
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "PRODUCT_ID", nullable = false)
    Product product;
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "CREATE_TS")
    protected Date timestamp;
    @Lob
    @Column(name = "NAME")
    protected String name = "";
    @Lob
    @Column(name = "EMAIL")
    protected String email = "";
    @Basic
    @Column(name = "IP_ADDRESS")
    protected String ipAddress = "";


    public Comment() {
        timestamp = Calendar.getInstance().getTime();
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getIpAddress() {
        return ipAddress;
    }

    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Product getProduct() {
        return product;
    }

    @XmlTransient
    public void setProduct(Product product) {
        this.product = product;
    }

    public Date getTimestamp() {
        return timestamp;
    }
}

